﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AddSchedule : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["id"] != null)
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                DropDownList1.SelectedValue = id.ToString();
                DropDownList1.Enabled = false;
            }

        }
        catch (Exception)
        {

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            AdminBAL _adminBal = new AdminBAL();
            int routeid = Convert.ToInt32(DropDownList1.SelectedValue);
            string dayofWeek = "";
            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                {
                    dayofWeek += item.Value + ",";
                }
            }
            dayofWeek = dayofWeek.Remove(dayofWeek.Length - 1);
            TimeSpan t = TimeSpan.Parse(TextBox1.Text);
            string time = t.Hours + ":" + t.Minutes;
            _adminBal.AddSchedule(routeid, dayofWeek, time);
            Response.Redirect("~/Admin/ScheduleList.aspx");
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string value = "";
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                value += item.Value;
            }
        }
        if (value.Length < 3)
        {
            args.IsValid = false;
        }
        else
        {
            try
            {
                TimeSpan t = TimeSpan.Parse(TextBox1.Text);
                if (t.TotalHours < 0 || t.TotalHours >= 24)
                {
                    args.IsValid = false;
                }
                else
                {
                    args.IsValid = true;
                }
            }
            catch (Exception)
            {
                args.IsValid = false;
            }
        }
    }

}
